package bb.lanxing.lib.devices.sprint.utils;

import android.text.TextUtils;
import bb.lanxing.lib.devices.sprint.SprintFile;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class SprintFileHelper {
    public static final String FILE_LIST = "filelist.txt";
    public static final String FILE_LIST_TMP = "filelist.tmp";
    public static final String MAP_FILE_NAME = "maplist.txt";
    public static final String NAV_FILE_NAME = "routelist.txt";
    public static final String NAV_FILE_STUFFIX = ".ro";
    public static final String SETTINGS_FILE_NAME = "Setting.json";

    public static List<SprintFile> parseFileList(String str, List<String> list) {
        if (list == null) {
            return null;
        }
        ArrayList arrayList = new ArrayList();
        for (String str2 : list) {
            String[] split = str2.split(" ");
            if (split.length >= 2) {
                try {
                    String str3 = split[0];
                    arrayList.add(new SprintFile(str3, str + File.separator + split[0], Long.parseLong(split[1])));
                } catch (Exception unused) {
                }
            }
        }
        return arrayList;
    }

    public static List<SprintFile> parseFileList(String str, String str2) {
        return parseFileList(str, readLings(str2));
    }

    public static List<String> readLings(String str) {
        if (str == null) {
            return null;
        }
        ArrayList arrayList = new ArrayList();
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(str)));
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine == null) {
                    break;
                }
                arrayList.add(readLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return arrayList;
    }

    public static List<SprintFile> parseMapList(String str) {
        if (str == null) {
            return null;
        }
        String lowerCase = str.trim().toLowerCase();
        if (TextUtils.isEmpty(lowerCase)) {
            return null;
        }
        File file = new File(lowerCase);
        if (file.exists()) {
            return parseFileList(file.getParent(), readLings(lowerCase));
        }
        return null;
    }

    public static List<SprintFile> parsetNavList(String str) {
        if (str == null) {
            return null;
        }
        String trim = str.trim();
        File file = new File(trim);
        if (file.exists()) {
            return parseFileList(file.getParent(), trim);
        }
        return null;
    }

    public static boolean isFileList(String str) {
        return str != null && str.endsWith(FILE_LIST);
    }

    public static boolean isSettingFile(String str) {
        return str != null && str.endsWith(SETTINGS_FILE_NAME);
    }

    public static boolean isMapListFile(String str) {
        return str != null && str.endsWith(MAP_FILE_NAME);
    }

    public static boolean isNavListFile(String str) {
        return str != null && str.endsWith(NAV_FILE_NAME);
    }

    public static boolean isNavFile(String str) {
        return str != null && str.endsWith(NAV_FILE_STUFFIX);
    }

    public static void clearNavList(String str) {
        File file = new File(str, NAV_FILE_NAME);
        if (file.exists()) {
            file.delete();
        }
    }

    public static void clearMapsList(String str) {
        if (str == null) {
            return;
        }
        File file = new File(str, MAP_FILE_NAME);
        if (!file.exists()) {
            return;
        }
        file.delete();
    }

    public static void clearFiles(String str) {
        clearNavList(str);
        File file = new File(str, FILE_LIST);
        if (file.exists()) {
            file.delete();
        }
        File file2 = new File(str, MAP_FILE_NAME);
        if (file2.exists()) {
            file2.delete();
        }
        File file3 = new File(str, SETTINGS_FILE_NAME);
        if (file3.exists()) {
            file3.delete();
        }
    }
}
